package com.framework.dbs.orm;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ibatis.sqlmap.engine.execution.SqlExecutor;
import com.ibatis.sqlmap.engine.mapping.statement.RowHandlerCallback;
import com.ibatis.sqlmap.engine.scope.StatementScope;

public class IBatisSqlExecutor extends SqlExecutor {
	private static final Log logger = LogFactory.getLog(IBatisSqlExecutor.class);
    private IDialect dialect;
    private boolean enableCustomizedPaged;

    public IBatisSqlExecutor() {
        enableCustomizedPaged = true;
    }

    public IDialect getDialect() {
        return dialect;
    }

    public void setDialect(IDialect dialect) {
        this.dialect = dialect;
    }

    public boolean isEnableCustomizedPaged() {
        return enableCustomizedPaged;
    }

    public void setEnableCustomizedPaged(boolean enableCustomizedPaged) {
        this.enableCustomizedPaged = enableCustomizedPaged;
    }

    public void executeQuery(StatementScope request, Connection conn, String sql, 
    		Object parameters[], int skipResults, int maxResults, RowHandlerCallback callback) throws SQLException {
        if((skipResults != 0 || maxResults != 0xfff0bdc1) && supportsPaged()) {
            sql = dialect.getPagedString(sql, skipResults, maxResults);
            if(logger.isDebugEnabled()) {
                logger.debug(sql);
            }
            skipResults = 0;
            maxResults = 0xfff0bdc1;
        }
        super.executeQuery(request, conn, sql, parameters, skipResults, maxResults, callback);
    }

    public boolean supportsPaged() {
        if(enableCustomizedPaged && dialect != null) {
            return dialect.supportsPaged();
        } else {
            return false;
        }
    }
    
}
